package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.R;
import com.itheima.config.SmsUtils;
import com.itheima.pojo.C;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 登录校验
     *
     * @param c
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R d(@RequestBody C c, HttpServletRequest request) {

        //获取发送的验证码
        // Object code = request.getSession().getAttribute("code");

        //使用redis获取验证码
        Object code = redisTemplate.opsForValue().get(c.getPhone());

        //校验
        if (c.getCode().equals(code + "") && c.getCode() != null && code != null) {

            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("phone", c.getPhone());
            //查询是否第一次登录
            User user = userService.getOne(wrapper);
            if (user == null) {
                //如果第一次登录将数据存入数据库
                user = new User();
                user.setPhone(c.getPhone());
                userService.save(user);
            }
            request.getSession().setAttribute("userId", user.getId());

          //  redisTemplate.opsForValue().set("userId", user.getId().toString());

            // redisTemplate.delete("code");
            return R.success(null);
        }
        return R.error("验证码错误");
    }

    /**
     * 发送验证码
     *
     * @param map
     * @param request
     * @return
     */
    @PostMapping("/sendMsg")
    public R send(@RequestBody Map<String, String> map, HttpServletRequest request) {

        String phone = map.get("phone");

        log.info("接收到的手机号" + phone);
        //获取随机验证码
        String code = RandomStringUtils.randomNumeric(5);
        //将验证码存入session
        // request.getSession().setAttribute("code", code);
        //存入redis
        redisTemplate.opsForValue().set(phone, code, 3l, TimeUnit.MINUTES);
        //调用工具类发送验证码
        //SmsUtils.sendSms(phone,code);
        log.info("此次验证码为====>" + code);

        return R.success(null);

    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    @PostMapping("/loginout")
    public R drop(HttpServletRequest request) {
        //用户退出结束此次会话
        HttpSession session = request.getSession();
        session.invalidate();

        return R.success(null);
    }

}
